using UnityEngine;
using System.Collections;

public class MenuControl : MonoBehaviour {
	
	#region Attributes
	
	[HideInInspector]
	public bool paused = false;
	//[HideInInspector]
	public bool dead = false;
    private bool pause = false;
	
	public Vector2 deathBKGPosition = Vector2.zero;
	public Vector2 pauseBKGPosition = Vector2.zero;
	public Vector2 retryButtonPosition = Vector2.zero;
	public Vector2 pauseButtonPosition = Vector2.zero;
	public Vector2 continueButtonPosition = Vector2.zero;
	
	public Texture2D deathBKG = null;
	public Texture2D pauseBKG = null;
	public Texture2D retryButton = null;
	public Texture2D pauseButton = null;
	public Texture2D exitButton = null;
	public Texture2D optionsButton = null;
	public Texture2D continueButton = null;
	
	private Rect deathWindowRect;
	private Rect pauseWindowRect;
	private Rect retryButtonRect;
	private Rect continueButtonRect;
	private Rect exitButtonRect;
	private Rect pauseButtonRect;
	
	private GUIStyle buttonStyle;

	
	#endregion
	
	#region Methods
	#endregion
	
	#region Unity Methods
	void Start () {
		
		InitButtonStyle();
		InitButtonsRect();
		
	}
	
	void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            this.pause = !this.pause;
            this.DoPause();
        }
	}

    private void DoPause()
    {
        if (this.pause)
            paused = GameControl.PauseGame(paused);
        else
            paused = GameControl.UnpauseGame(paused);
    }
	
	void OnGUI(){
		
		SetButtonsRect();
		
        //if (GUI.Button(pauseButtonRect,pauseButton,buttonStyle))
        //{
        //    Debug.Log("Pause pressed");
        //    paused = GameControl.PauseGame(paused);
        //    this.pauseWindowRect = GUI.Window(WindowIDConstants.PAUSE_MENU_ID, this.pauseWindowRect, PauseWindowFuntion, pauseBKG,buttonStyle);
        //}

        if (GUI.Button(pauseButtonRect, pauseButton, buttonStyle))
        {
            this.pause = true;
            this.DoPause();
        }

        if (pause)
        {
            Debug.Log("Pause pressed");
            this.pauseWindowRect = GUI.Window(WindowIDConstants.PAUSE_MENU_ID, this.pauseWindowRect, PauseWindowFuntion, pauseBKG, buttonStyle);
        }

        
		
        if(dead)
		{
			paused = GameControl.PauseGame(paused);
			this.deathWindowRect = GUI.Window(WindowIDConstants.DEATH_MENU_ID, this.deathWindowRect, DeathWindowFuntion, deathBKG,buttonStyle);
		}
		
	}
	#endregion
	
	#region Methods
	private void DeathWindowFuntion(int id)
	{
		if(GUI.Button(retryButtonRect,retryButton,buttonStyle))
		{
			this.dead = false;
			RestartScene();
			this.paused = GameControl.UnpauseGame(paused);
		}
	
	}
	
	private void PauseWindowFuntion(int id)
	{
		//this.paused = GameControl.PauseGame(paused);
		Debug.Log("Pause");
		if(GUI.Button(continueButtonRect,continueButton,buttonStyle))
		{
			this.paused = GameControl.UnpauseGame(paused);
            pause = false;
		}
	}
	
	private void RestartScene(){
		
		Application.LoadLevel(Application.loadedLevelName);
		
	}
	
	private void InitButtonStyle(){
		
		buttonStyle = new GUIStyle();
		buttonStyle.imagePosition = ImagePosition.ImageOnly;
		
	}
	
	private void InitButtonsRect(){
		
		/*deathWindowRect = new Rect(Screen.width/2-deathBKG.width/4,Screen.height/2-deathBKG.height/4, deathBKG.width/2,deathBKG.height/2);
		pauseWindowRect = new Rect(Screen.width/2-pauseBKG.width/4,Screen.height/2-pauseBKG.height/4, pauseBKG.width/2,pauseBKG.height/2);
		retryButtonRect = new Rect(Screen.width/2-retryButton.width,Screen.height/2-retryButton.height-offset, retryButton.width/1.5f,retryButton.height/1.5f);*/
		
		deathWindowRect = new Rect(deathBKGPosition.x, deathBKGPosition.y, deathBKG.width/2,deathBKG.height/2);
		pauseWindowRect = new Rect(pauseBKGPosition.x, pauseBKGPosition.y, pauseBKG.width/2,pauseBKG.height/2);
		retryButtonRect = new Rect(retryButtonPosition.x, retryButtonPosition.y, retryButton.width/1.5f,retryButton.height/1.5f);
		continueButtonRect = new Rect(continueButtonPosition.x, continueButtonPosition.y, continueButton.width/1.5f,continueButton.height/1.5f);
		pauseButtonRect = new Rect(pauseButtonPosition.x, pauseButtonPosition.y, pauseButton.width/3.0f,pauseButton.height/3.0f);

		
	}
	
	private void SetButtonsRect(){
		
		deathWindowRect.Set(deathBKGPosition.x, deathBKGPosition.y, deathBKG.width/2,deathBKG.height/2);
		pauseWindowRect.Set(pauseBKGPosition.x, pauseBKGPosition.y, pauseBKG.width/2,pauseBKG.height/2);
		retryButtonRect.Set(retryButtonPosition.x, retryButtonPosition.y, retryButton.width/1.5f,retryButton.height/1.5f);
		continueButtonRect.Set(continueButtonPosition.x, continueButtonPosition.y, continueButton.width/1.5f,continueButton.height/1.5f);
		pauseButtonRect.Set(pauseButtonPosition.x, pauseButtonPosition.y, pauseButton.width/3.0f,pauseButton.height/3.0f);

		
	}
	#endregion
}
